(function($){
    function initSetState() {
        $('.tag_set_state').on('click', function(){
            var obj = $(this);
            var state = obj.attr('dstate');
            var id = obj.attr('did');
            var url = obj.attr('durl');
            var info = '删除';
            switch(state) {
                case '-1':
                    info = '删除';
                    break;
                case '1':
                    info = '恢复正常';
                    break;
            }
            layer.confirm(
                '确定要【' + info + '】此标签吗？',
                {
                    icon: 3,
                    title: '系统提示',
                    btn: ['确 定', '取 消']
                },
                function(index){
                    layer.close(index);
                    layer.load();
                    $.post(
                        url,
                        {id: id, state: state},
                        function(data){
                            if(data.status == 10000) {
                                location.reload(true);
                            } else {
                                layer.closeAll();
                                layer.alert(data.errmsg);
                            }
                        },
                        'json'
                    );
                },
                function(index){
                    layer.close(index);
                }
            );
        });
    }
    function formResult(msg, state) {
        layer.closeAll();
        if(state == 0) {
            layer.alert(msg, {icon: 5});
        } else {
            layer.confirm('编辑成功！', {icon: 6, title:'系统提示', btn: ['确定']}, function(index){
                location.reload(true);
            });
        }
    }
    function initUpdate()
    {
        $('.tag_update').on('click', function(){
            var obj = $(this);
            var id = obj.attr('did');
            if(id<1){
            	formResult("error!",false);
            	return;
            }
            layer.open({
                type: 2,
                area: ['700px', '530px'],
                fix: false, //不固定
                maxmin: true,
                content: 'update?id='+id
            });
        });
    }
    $(function(){
        layer.config({extend: 'extend/layer.ext.js'});
        window.formResult = formResult;
        initSetState();
        initUpdate();
    });
})(jQuery);